package com.gestocasa.model;

import java.util.UUID;
import java.util.Vector;

public class Area implements Comparable<Area> {

	private int id;
	private UUID internalId;
	private String name;
	private int order;
	private Vector<Attribute> attributes;
	private Vector<Configuration> configurations;
	
	public Area() {
		this(null);
	}
	
	public Area(Area a) {
		attributes = new Vector<Attribute>();
		configurations = new Vector<Configuration>();
		
		if(a == null) {
			id = -1;
			internalId = UUID.randomUUID();
			name = "";
			order = 0;
		}
		else {
			id = a.getId();
			internalId = a.getInternalId();
			name = a.getName();
			order = a.getOrder();
			
			if(a.getAttributes() != null) {
				for(Attribute at : a.getAttributes()) {
					attributes.add(new Attribute(at));
				}
			}
			
			if(a.getConfigurations() != null) {
				for(Configuration c : a.getConfigurations()) {
					configurations.add(new Configuration(c));
				}
			}
		}
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public UUID getInternalId() {
		return internalId;
	}

	public void setInternalId(String internalId) {
		setInternalId((internalId == null) ? UUID.randomUUID() : UUID.fromString(internalId));
	}
	
	public void setInternalId(UUID internalId) {
		this.internalId = internalId;
	}
	
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getOrder() {
		return order;
	}

	public void setOrder(int order) {
		this.order = order;
	}

	public Attribute getAttribute(String id) {
		for(Attribute a : attributes) {
			if(a.getInternalId().toString().equals(id)) {
				return a;
			}
		}
		return null;
	}
	
	public Vector<Attribute> getAttributes() {
		return attributes;
	}

	public void setAttributes(Vector<Attribute> attributes) {
		this.attributes = attributes;
	}

	public Vector<Configuration> getConfigurations() {
		return configurations;
	}

	public void setConfigurations(Vector<Configuration> configurations) {
		this.configurations = configurations;
	}

	public boolean equals(Area area) {
		return (area == null) ? false : internalId.equals(area.getInternalId());
	}
	
	@Override
	public int compareTo(Area o) {
		if(order == o.getOrder()) {
			if(name == null || o.getName() == null) {
				return 0;
			}
			else {
				return name.compareTo(o.getName());
			}
		}
		else {
			if(order < o.getOrder()) {
				return -1;
			}
			else if(order > o.getOrder()) {
				return 1;
			}
			else {
				return 0;
			}
		}
	}
}
